

import 'package:flutter/material.dart';
import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/model/variety_model.dart';

class VarietyPage extends StatefulWidget {
  final int type;

  const VarietyPage({Key key, this.type}) : super(key: key);
  @override
  _VarietyPageState createState() => _VarietyPageState();
}

class _VarietyPageState extends State<VarietyPage> with AutomaticKeepAliveClientMixin {

  bool get wantKeepAlive => true;
  Future _futureBuilderFuture;
  VarietyModel varietyModel;
  String errorImgUrl = "https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2596164201.jpg";
  String _area = "全部";
  String _year = "全部";

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: 
      commonFutureBuilder(
        context,
        future: _futureBuilderFuture,
        callWidget: bodyWidget(),
        callTap: init
      ),
    );
  }

  Widget bodyWidget(){
    return varietyModel != null
    ? CustomScrollView(
      reverse: false,
      slivers: [
        SliverGrid.count(
          crossAxisCount: 3,
          mainAxisSpacing: 3.0,
          crossAxisSpacing: 3.0,
          childAspectRatio: 0.6,
          children: _widgetCount()
        )
      ]
    ) 
    : Container();
  }

  List<Widget> _widgetCount(){
    List<Widget> _list = [];
    for (var i = 0; i < varietyModel.data.items.length; i++) {
      Widget _addWidget = InkResponse(
        highlightColor: Colors.transparent,
        radius: 0,
        onTap: (){
          // Get.toNamed('/VideoDetailPage', arguments: {"videoId": varietyModel.data.items[i].videoId});
        },
        child: Container(
          child: Stack(
            children: <Widget>[
              Container(
                height: double.infinity,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(4))
                ),
                child: Column(
                  children: <Widget>[
                    Container(
                      height: ScreenUtil.getInstance().screenWidth / 3 / 0.75,
                      child: Stack(
                        children: <Widget>[
                          Container(
                            width: double.infinity,
                            height: ScreenUtil.getInstance().screenWidth / 3 / 0.75,
                            child: varietyModel.data.items[i].imgUrl != errorImgUrl
                            ? loadNetworkImage(varietyModel.data.items[i].imgUrl)
                            : loadAssetImage("img/placeholder"),
                          ),
                          varietyModel.data.items[i].updateText != null 
                          ? Positioned(
                            bottom: 0,
                            left: 0,
                            right: 0,
                            child: Container(
                              height: ScreenUtil().getWidthPx(60),
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
                              color: Color.fromRGBO(0, 0, 0, 0.7),
                              child: Cptext(text: varietyModel.data.items[i].updateText, style: TextStyle(color: Colors.white, fontSize: 12),),
                            )
                          )
                          : Container()
                        ],
                      ),
                    ),
                    Expanded(
                      child: Container(
                        alignment: Alignment.center,
                        child: Cptext(text:varietyModel.data.items[i].title, style: TextStyle(fontSize: 14),),
                      ),
                    )
                  ],
                ),
              ),
            ],
          )
        ),
      );
      _list.add(_addWidget);
    }
    return _list;
  }

  //网络初始化
  init(){
    // initSwiper();
    setState(() {
      _futureBuilderFuture = initGetIndex();
    });
  }

  /**
   * 网络请求
   */
  initGetIndex() async {
    // String url = "http://api.tgmmvip.com/wolong/gae/video/list?area=&size=20&year=&page=0&type=${widget.type}";
    // var _res = await HttpUtil().request(url, method: 'get', need_login: false);
    // logger.d(_res);
    // setState(() {
    //   varietyModel = VarietyModel.fromJson(_res);
    // });
    // if(_res != null) {
    //   return _res;
    // } else {
    //   return null;
    // }
    return false;
  }
}